#include"worker.h"
#include"handleCommand.h"
#include"transMsg.h"
int business(int netfd){
    //train_t train;
    char command[4096] = {0};

    //验证密码
    char username[1024]= {0};
    serverPwdAuth(netfd,username);
    SYSTEM_LOG(LOG_NOTICE,username,"Connection successful");
    //printf("username = %s\n",username);


    //创建储存文件的目录                                                                                 
    char userDisk[256] = {0};
    sprintf(userDisk,"%s/%s","netDisk",username);
    //printf("userDisk = %s\n",userDisk);
    mkdir("netDisk",0777);
    mkdir(userDisk,0777);
    //perror("mkdir");

    pathStack_t pathStack;
    pathStackInit(&pathStack);
    while(1){
        int exitFlag = 0;
        //printf("before recvCommand netfd = %d\n",netfd);
        int ret = recvCommand(netfd,command);
        SYSTEM_LOG(LOG_NOTICE,username,command);
        if(ret == -1){
            printf("1 client left!\n");
            break;
        }
        //printf("command =%s\n",command);
        exitFlag = serverExecCommand(netfd,&pathStack,command,username);
        if(exitFlag == 1){
            break;
        }
        SYSTEM_LOG(LOG_NOTICE,username,"Operation successful");
    }
    SYSTEM_LOG(LOG_NOTICE,username,"Disconnection");
    return 0;
}

int serverPwdAuth(int netfd,char* username){
    train_t train;
    while(1){
        //接收用户名
        char recveduser[256] = {0};
        memset(&train,0,sizeof(train_t));
        recvn(netfd,&train.length,sizeof(train.length));
        recvn(netfd,train.data,train.length);
        memcpy(recveduser,train.data,train.length);
        memcpy(username,recveduser,train.length);

        //printf("train.length = %d,train.data=%s,recveduser =%s\n",train.length,train.data,recveduser);
        struct spwd* sp;
        sp = getspnam(recveduser);
        if(sp == NULL){
            //通知客户端
            memset(&train,0,sizeof(train_t));
            train.length = strlen("-1");
            memcpy(train.data,"-1",train.length);
            send(netfd,&train.length,sizeof(train.length),0);
            send(netfd,train.data,train.length,0);
            continue;
        }
        // sp!=NULL,获得盐值(salt)
        char salt[1024] = {0};
        char secret[1024];
        memcpy(secret,sp->sp_pwdp,strlen(sp->sp_pwdp));
        int i=0,j=0;
        for(i=0,j=0;secret[i]&& j!=3;++i){
            if(secret[i]== '$'){
                ++j;
            }
        }
        memcpy(salt,secret,i-1);
        //printf("salt = %s\n",salt);
        //将salt发送给客户端
        memset(&train,0,sizeof(train_t));
        train.length =strlen(salt);
        memcpy(train.data,secret,train.length);
        send(netfd,&train.length,sizeof(train.length),0);
        send(netfd,train.data,train.length,0);
        //接收客户端发来的密文密码
        memset(&train,0,sizeof(train_t));
        recvn(netfd,&train.length,sizeof(train.length));
        recvn(netfd,train.data,train.length);
        //printf("train.length = %d,recvedSecret =%s\n",train.length,train.data);

        //判断密码是否正确
        if(memcmp(sp->sp_pwdp,train.data,train.length)==0){
            //密码正确，通知客户端
            memset(&train,0,sizeof(train_t));
            train.length = strlen("0");
            memcpy(train.data,"0",train.length);
            send(netfd,&train.length,sizeof(train.length),0);
            send(netfd,train.data,train.length,0);
            return 0;
        }else{
            //密码错误，通知客户端
            memset(&train,0,sizeof(train_t));
            train.length = strlen("-1");
            memcpy(train.data,"-1",train.length);
            send(netfd,&train.length,sizeof(train.length),0);
            send(netfd,train.data,train.length,0);
            continue;
        }

    }
    return -1;
}
